package com.note7_6;

import java.io.*;

public class Question4 {

	/*
	 *	4:获得文件行数的方法有 bufferedReader.readLine 方法 或者查找byte是否=='\n',比较谁快！
	 */
	
	public static int bufferedReaderLine(String path) throws IOException{
		BufferedReader bfr = new BufferedReader(new FileReader(path));
		int flag = 0;
		while(bfr.readLine() != null){
			flag++;
		}
		if(bfr != null){
			bfr.close();
		}
		return flag;
	}
	
	public static int byteReaderLine(String path) throws IOException{
		int flag = 0;
		InputStream is = new FileInputStream(path);
		byte[] bs = new byte[1024];
		while((is.read(bs)) != -1){
			for(byte b:bs){
				if(b == '\n'){
					flag++;
				}
			}
		}
		if(is != null){
			is.close();
		}
		return flag+1;
	}
	
	public static void main(String[] args) throws IOException {
		String path = "D:/迅雷下载/hyxd/LocalData/Patch/new_patchlist_windows_normal.txt";
		long startTime = System.currentTimeMillis();
		int flag = byteReaderLine(path);
		long endTime = System.currentTimeMillis();
		int flag0 = bufferedReaderLine(path);
		long bfEndTime = System.currentTimeMillis();
		long byteTime = endTime - startTime;
		long bfTime =bfEndTime - endTime;
		System.out.println("使用byte查找的结果为："+flag);
		System.out.println("使用byte查找时间："+byteTime);
		System.out.println("使用bufferedReader.readLine查找的结果为："+flag);
		System.out.println("使用bufferedReader.readLine查找时间为："+bfTime+"\n");
		if(flag == flag0){
			if(byteTime > bfTime){
				System.out.println("使用 bufferedReader.readLine快");
			}else{
				System.out.println("使用byte查找快");
			}
		}
	}
}
